$(function () {
    $("body").on('click','.user-shop-cart-box',function () {
        //加载购物车数据内容
        getUserShopCartList(function (result) {
            var context = '<div class="text-center"><span class="noShopCart"></span><h6><span class="fs12 c-999">购物车空空如也，快<a href="/web/course/courseList" class="c-success">去选课</a>吧~</span></h6></div>';
            if(result!=null && result.length>0){
                var buyNum = 0;
                $.each(result,function (index,e) {
                    buyNum += e.buyNum;
                });
                context='<div>';
                context+='  <h6 class="fs12 c-999">购物车共有<span class="fs12 c-danger shop-cart-num">'+buyNum+'</span>个商品</h6>';
                context+='<div class="head-sc-item-list pt10">';
                $.each(result,function (index,e) {
                    context+='<section class="head-sc-item mb20" id="shop-cart-head-'+e.shopId+'-'+e.shopType+'">';
                    context+='  <div class="media">';
                    var urls = '';
                    if(e.shopType=='BOOK'){
                        urls = baseloaction +'/web/book/info?bookId='+e.shopId;
                    }else if(e.shopType=='COURSE'){
                        urls = baseloaction +'/web/course/courseInfo?courseId='+e.shopId;
                    }else{
                        urls = 'javascript:void(0);';
                    }
                    if(e.shopType=='BOOK' || e.shopType=='COURSE'){
                        context+='      <div class="media-left">';
                        var imgUrl = '';
                        var imageMap = null;
                        if(e.shopType=='BOOK'){
                            imageMap = e.shop.book.imageMap;
                        }
                        if(e.shopType=='COURSE'){
                            imageMap = e.shop.course.imageMap;
                        }
                        if(typeof imageMap !='undefined' && typeof imageMap.pcUrlMap !='undefined' && imageMap!=null){
                            imgUrl = imageMap.pcUrlMap.small;
                        }
                        context+='          <a href="'+urls+'">';
                        context+='              <img class="media-object lazy" alt="'+e.shop.shopName+'" width="90" height="50" src="'+imgUrl+'" data-holder-rendered="true"/>';
                        context+='          </a>';
                        context+='      </div>';
                    }

                    context+='      <div class="media-body">';
                    context+='          <div class="sc-item-title">';
                    context+='              <a href="'+urls+'" class="c-333 fs14">'+e.shop.shopName+'</a>';
                    context+='          </div>';
                    context+='      </div>';
                    context+='      <div class="media-right">';
                    context+='          <section class="text-right" style="width: 80px;">';
                    context+='              <p><span class="fs14 c-danger">￥'+e.shop.realPrice+'</span></p>';
                    context+='              <a href="javascript:void(0);" data-shop-id="'+e.id+'" class="del-shop-cart-box fs14 c-ccc" title="删除"><i class="fa fa-trash-o"></i></a>';
                    context+='           </section>';
                    context+='      </div>';
                    context+='  </div>';
                    context+='</section>';
                });
                context +='     </div>';
                context +='</div>';
                context += '<div class="head-sc-kc-foot text-center">';
                context +='<a href="'+baseloaction+'/web/uc/order/initPayOrder?orderType=COMMON">';
                context += '    <button type="button" class="btn btn-w-m btn-danger unRadius"><span class="fs16">去结算</span></button>';
                context +='</a>';
                context += '</div>';
            }
            $("#head-shop-list-box").html(context);
            //图片数据懒加载
            lazyCourseImage();
            //删除购物车
            $(".del-shop-cart-box").click(function () {
                var _this = $(this);
                var shopCartId = _this.data('shop-id');
                //删除购物车
                deleteShopCart(shopCartId,function () {
                    _this.parents('section.head-sc-item').remove();
                });
            });
        });
    });

    /**
     * 加载用户显示的购物车数据内容
     * @param callback 回调方法
     */
    function getUserShopCartList(callback){
        $.ajax({
            url: baseloaction +'/web/uc/shopCar/loadShowUserShopCartList',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    if($.isFunction(callback)){
                        callback(result.entity);
                    }
                }else{
                    //icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，加载购物车数据失败');
            }
        });
    }

    /**
     * 获取购物车中商品数量
     */
    function getShopCartNum() {
        var urls = baseloaction + '/web/uc/shopCar/ajax/queryShopCartNum';
        $.ajax({
            url: urls,
            type: "post",
            dataType: "json",
            success: function(result) {
                var count = parseInt(result.entity);
                if(count>0){
                    $(".head-keNum-pointer").removeClass('hide');
                    $(".head-keNum-pointer").text(count);
                }
            },
            error: function(error){
                icModale(0,'','系统繁忙，请求失败');
            }
        });
    }
    //获取购物车中商品数量
    getShopCartNum();
});

/**
 * 添加购物车
 * @param shopId 商品ID
 * @param shopType 商品类型
 * @param buyNum 购买量
 */
function addShopCart(shopId,shopType,buyNum){
    var shareKey = getParameter("shareKey");
    isLogin(function () {
        $.ajax({
            url:baseloaction +'/web/uc/shopCar/addShopCart',
            type:'post',
            dataType:'json',
            data:{
                'shopId':shopId,
                'shopType':shopType,
                'buyNum':buyNum,
                'shareKey':shareKey
            },
            success:function (result) {
                if(result.success){
                    var count = $(".head-keNum-pointer").text();
                    if(count!='' && count!=null && count!='undefined'){
                        count = parseInt(count)+1;
                        if(count>0){
                            $(".head-keNum-pointer").text(count);
                            $(".head-keNum-pointer").removeClass('hide');
                        }
                    }
                }
                icModale(0,'',result.message);
            },
            error:function (error) {
                icModale(0,'','系统错误，添加购物车失败');
            }
        });
    });
}

/**
 * 删除购物车数据
 * @param shopCartId  购物车ID
 * @param callback 回调
 */
function deleteShopCart(shopCartId,callback) {
    $.ajax({
        url: baseloaction +'/web/uc/shopCar/deleteShopCart',
        type: "post",
        data:{
            'shopCartIds':shopCartId+''
        },
        dataType: "json",
        success: function(result) {
            if(result.success==true){
                if($.isFunction(callback)){
                    icModale(0, "", "删除成功");
                    callback();
                }
                var count = $(".head-keNum-pointer").text();
                if(count!='' && count!=null && count!='undefined'){
                    count = parseInt(count)-1;
                    if(count>0){
                        $(".head-keNum-pointer").text(count);
                    }else {
                        $(".head-keNum-pointer").text(count);
                        $(".head-keNum-pointer").addClass('hide');
                    }
                }
            }else{
                icModale(0, "", result.message);
            }
        },
        error: function(error){
            icModale(0, "", "系统错误，删除购物车商品失败");
        }
    });
}